package com.cy.embeddingdemo.util;
import java.io.*;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class MinerUutil {
    // 下载文件的方法
    public static void downloadFile(String fileURL, String savePath) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(fileURL))
                .GET()
                .build();

        HttpClient client = HttpClient.newHttpClient();
        HttpResponse<Path> response = client.send(request, HttpResponse.BodyHandlers.ofFile(Paths.get(savePath)));

        System.out.println("Downloaded file to: " + savePath);
    }

    // 删除文件的方法
    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists() && file.delete()) {
            System.out.println("Deleted file: " + filePath);
        } else {
            System.out.println("Failed to delete file: " + filePath);
        }
    }

    /**
     * 递归删除文件夹及其所有内容
     * @param directory 要删除的文件夹路径
     * @throws IOException 如果删除过程中发生错误
     */
    public static void deleteDirectory(Path directory) throws IOException {
        // 使用 Files.walkFileTree 遍历文件夹及其所有内容
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // 删除文件
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                // 删除文件夹
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }

        // 解压 ZIP 文件的方法
    public static void unzipFile(String zipFilePath, String outputFolder) throws IOException {
        File destDir = new File(outputFolder);
        if (!destDir.exists()) {
            destDir.mkdir();
        }

        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath))) {
            ZipEntry entry = zipIn.getNextEntry();
            while (entry != null) {
                String filePath = outputFolder + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    // 如果是文件，则提取文件
                    File newFile = new File(filePath);
                    new File(newFile.getParent()).mkdirs();
                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile))) {
                        byte[] bytesIn = new byte[4096];
                        int read = 0;
                        while ((read = zipIn.read(bytesIn)) != -1) {
                            bos.write(bytesIn, 0, read);
                        }
                    }
                } else {
                    // 如果是目录，则创建目录
                    File dir = new File(filePath);
                    dir.mkdirs();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
        }

        System.out.println("Unzipped to: " + outputFolder);
    }
}
